package routers

import (
	"net/http"

	"shqsoft.cn/pas3ui/base"
	"shqsoft.cn/pas3ui/entity"

	jwt "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
)

// FlowDoingSum
type FlowDoing struct {
	List  []FlowDoingSum `json:"list"`
	Total int            `json:"total"`
}

type FlowDoingSum struct {
	WFID   int    `json:"WFID"`
	EMID   int    `json:"EMID"`
	NUM    int    `json:"NUM"`
	STEPID int    `json:"STEPID"`
	WFNM   string `json:"WFNM"`
	PermID int    `json:"PermID"`
}

// FlowDoingSumRouter
func FlowDoingSumRouter(c echo.Context) error {
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*UserLoginClaims)
	var flowDoingSum []entity.FlowDoingSum
	err := base.MYDB.Where(`EMID=?`, claims.UserID).Find(&flowDoingSum).Error
	if err != nil {
		return err
	}
	flen := len(flowDoingSum)
	if flen == 0 {
		return nil
	}
	FlowDoingSum2 := make([]FlowDoingSum, 0, flen)
	for i := 0; i < flen; i++ {
		var f2 FlowDoingSum
		f2.EMID = flowDoingSum[i].EMID
		f2.NUM = flowDoingSum[i].NUM
		f2.PermID = flowDoingSum[i].PermID
		f2.STEPID = flowDoingSum[i].STEPID
		f2.WFID = flowDoingSum[i].WFID
		f2.WFNM = flowDoingSum[i].WFNM
		FlowDoingSum2 = append(FlowDoingSum2, f2)
	}
	flowDoing := FlowDoing{
		List:  FlowDoingSum2,
		Total: flen,
	}
	return c.JSON(http.StatusOK, flowDoing)
}
